#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 100010;
int stk[N], tt;

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int m; cin >> m;
	while (m--) {
		string op; cin >> op;
		if (op == "push") {
			int x; cin >> x;
			stk[++tt] = x;
		}else if (op == "pop") {
			tt--;
		}else if (op == "empty") {
			cout << (tt == 0 ? "YES" : "NO") << endl;
		}else if (op == "query") {
			cout << stk[tt] << endl;
		}
	}
	return 0;
}